package com.x.mvp.appbar;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.x.mvp.R;
import com.x.mvp.base.view.fragment.FragmentView;

public class AppBarFragment extends FragmentView<AppBarPresenter> {

    private static final String KEY_TYPE = "key_type";
    private static final String KEY_TITLE = "key_title";

    public static final int TYPE_MAIN_FRAGMENT = 0;
    public static final int TYPE_SECOND_FRAGMENT = 1;

    protected Toolbar toolbar;
    protected TextView titleView;
    private View.OnClickListener onNavigationClickListener;
    private View.OnClickListener onTitleClickListener;
    private int menuResId;
    private Toolbar.OnMenuItemClickListener menuItemClickListener;
    private int visibility = View.VISIBLE;
    private String title;
    private int backGroundColor = -1;

    public static Bundle getStartParams(int type, String title) {
        Bundle params = new Bundle();
        params.putInt(KEY_TYPE, type);
        params.putString(KEY_TITLE, title);
        return params;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.toolbar_actionbar;
    }

    @Override
    protected void initView() {
        toolbar = mainView.findViewById(R.id.toolbar_actionbar);
        titleView = mainView.findViewById(R.id.toolbar_title);
        int type = getArguments().getInt(KEY_TYPE, TYPE_MAIN_FRAGMENT);
        initView(type);
        if (backGroundColor != -1) {
            toolbar.setBackgroundResource(backGroundColor);
        }
    }

    protected void injectComponent() {
        getFragmentComponent().inject(this);
    }

    private void initView(int type) {
        switch (type) {
            case TYPE_MAIN_FRAGMENT:
                break;
            case TYPE_SECOND_FRAGMENT:
                toolbar.setNavigationIcon(R.drawable.ic_back);
                toolbar.setNavigationOnClickListener(onNavigationClickListener);
                break;
            default:
                break;
        }
        if (TextUtils.isEmpty(title)) {
            title = getArguments().getString(KEY_TITLE);
        }
        setTitle(title);
        if (menuResId > 0) {
            toolbar.inflateMenu(menuResId);
            toolbar.setOnMenuItemClickListener(menuItemClickListener);
        }
        if (onTitleClickListener != null) {
            titleView.setOnClickListener(onTitleClickListener);
        }
        mainView.setVisibility(visibility);
    }

    public AppBarFragment setNavigationOnClickListener(View.OnClickListener onClickListener) {
        onNavigationClickListener = onClickListener;
        return this;
    }

    public AppBarFragment setTitle(String title) {
        if (titleView != null) {
            titleView.setText(title);
        } else {
            this.title = title;
        }
        return this;
    }

    public AppBarFragment setVisible(int visibility) {
        if (mainView != null) {
            mainView.setVisibility(visibility);
        } else {
            this.visibility = visibility;
        }
        return this;
    }

    public AppBarFragment setNavigationIcon(Drawable drawable) {
        if (toolbar != null) {
            toolbar.setNavigationIcon(drawable);
        }
        return this;
    }

    public AppBarFragment setOnTitleClickListener(View.OnClickListener onClickListener) {
        this.onTitleClickListener = onClickListener;
        return this;
    }

    public AppBarFragment setMenuResId(int resId) {
        menuResId = resId;
        return this;
    }

    public AppBarFragment setOnMenuClickListener(Toolbar.OnMenuItemClickListener onClickListener) {
        menuItemClickListener = onClickListener;
        return this;
    }

    public AppBarFragment setBackGroundColor(int color) {
        this.backGroundColor = color;
        return this;
    }
}